- /* slflladd.cpp by K.Tsuru */
- // function ID = 207 DRADIX, BRADIX
- /************************************************************************
- SLong and SInteger classes
- It provides the sum m+n (m and n have same sign).
- *************************************************************************/
- #ifndef SN_H
- #include "sn.h"
- #endif
- static const char* const func = "\'+\' or LLAdd";
- SLong LLAdd(const SLong& m, const SLong& n){
- if(m.Sign() == 0) return n;
- if(n.Sign() == 0) return m;
- if(m.Sign() != n.Sign()) m.SetError(m.SYNTAX_ERR, func, 207);
- //It lets m >= n. See DDMult().
- if( m.aHead < n.aHead ) return LLAdd(n, m);
- if(m.Radix() != n.Radix()) m.SetError(m.RADIX_ERR, func, 207);
-
- SLong result(m); // result = m
- const fType* nv = n.ReadFigures();
- fType* rv = result.figure.Elements();
- fType rdx = m.Radix();
- fType w = 0; //work variable 2*(radix-1) + 1 <= fTypeMax = 65535
- //addition upto top figure of n
- uint i;
- for(i = n.aTail; i <= n.aHead ; i++){
- w += rv[i] + nv[i];
- rv[i] = w % rdx;
- w = (w >= rdx) ? 1 : 0; // w /= rdx; w <= 1
- }
-
- uint hm = m.aHead;
- #ifndef NDEBUG
- result.figure(hm);
- #endif
- //When w=0 it is not executed.
- //result=m; Then the upper part of "result" is the same as that of m.
- //Except the special case such as 9999 9999 ... 9999+1 it ends by a few times.
- for( ; w && (i <= hm) ; i++){
- w += rv[i]; // nv[i] = 0
- rv[i] = w % rdx;
- w = (w >= rdx) ? 1 : 0; // w /= rdx; w <= 1
- }
- if(w){ //carry w <= 1
- hm++;
- #ifndef NDEBUG
- assert(w <= 1);
- #endif
- result.Reserve(hm);
- rv = result.figure.Elements();
- result.figure[hm] = w; //carry to the top figure
- }
-
- //It detects the figure position.
- result.aHead = hm;
- uint rt = min(m.Tail(), n.Tail()); // aTail --> Tail() since ver 2.191
-
- while(!rv[rt]) rt++;
- result.aTail = rt;
- result.SetSign(m.Sign());
- return result;
- }
slflladd.cpp : last modifiled at 2017/03/13 14:32:00(1,890 bytes)
created at 2017/10/07 10:26:50
The creation time of this html file is 2017/11/09 14:52:03 (Thu Nov 09 14:52:03 2017).